<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:p="http://primefaces.org/ui">

    <ui:composition template="/template.xhtml">
        <ui:define name="title">
            <h:outputText value="#{bundle.CreateCompraTitle}"></h:outputText>
        </ui:define>

        <ui:define name="menuLateral">

            <h:form id="formLateralMenuCompraCreate">
                <h:panelGroup>
                    <p:slideMenu style="width: 120px">
                        <p:menuitem value="#{bundle.ListCompraTitle}" 
                                    action="#{compraController.prepareList}" ajax="false"
                                    icon="ui-icon-arrowreturnthick-1-w" />
                        <p:separator />
                        <p:menuitem value="#{bundle.CreateCompraSaveLink}" actionListener="#{compraController.create}"
                                    icon="ui-icon-disk" ajax="false" />
                        <p:menuitem value="Limpar" action="#{compraController.prepareCreate}"
                                    icon="ui-icon-document-b" ajax="false" />
                        <p:separator />
                        <p:submenu label="Cadastros Obrigatórios" icon="ui-icon-suitcase">
                            <p:submenu label="Pagamentos">
                                <p:menuitem id="menuLateralCondicaoPagamento" 
                                            value="#{bundle.CreateCondicaoPagamentoShowAllLink}"
                                            action="/paginasEntidades/condicaoPagamento/List" ajax="false"/>
                                <p:menuitem id="menuLateralFormaPagamento" 
                                            value="#{bundle.CreateFormaPagamentoShowAllLink}"
                                            action="/paginasEntidades/formaPagamento/List" ajax="false"/>
                            </p:submenu>
                            <p:submenu label="Participantes">
                                <p:menuitem id="menuLateralFornecedor" 
                                            value="#{bundle.CreateFornecedorShowAllLink}"
                                            action="/paginasEntidades/fornecedor/List" ajax="false"/>
                            </p:submenu>
                            <p:submenu label="Produtos">
                                <p:menuitem id="menuLateralProduto" 
                                            value="#{bundle.CreateProdutoShowAllLink}"
                                            action="/paginasEntidades/produto/List" ajax="false"/>
                                <p:menuitem id="menuLateralUnidadeMedida" 
                                            value="#{bundle.CreateUnidadeMedidaShowAllLink}"
                                            action="/paginasEntidades/unidadeMedida/List" ajax="false"/>
                                <p:menuitem id="menuLateralTipoProduto" 
                                            value="#{bundle.CreateTipoProdutoShowAllLink}"
                                            action="/paginasEntidades/tipoProduto/List" ajax="false"/>
                            </p:submenu>
                        </p:submenu>
                    </p:slideMenu>
                </h:panelGroup>
            </h:form>

        </ui:define>

        <ui:define name="body">
            <h:panelGroup id="messagePanel" layout="block">
                <p:growl />
            </h:panelGroup>

            <h:form id="formCompraCreate">
                <p:panelGrid id="pgCompraCreate" columns="7">
                    <h:outputLabel value="#{bundle.CreateCompraLabel_dataCompra}" for="dataCompra" />
                    <p:inputMask id="dataCompra" value="#{compraController.selected.dataCompra}" 
                                 title="#{bundle.CreateCompraTitle_dataCompra}" required="true" 
                                 requiredMessage="#{bundle.CreateCompraRequiredMessage_dataCompra}" 
                                 disabled="#{compraController.renderizaItensCompra}" 
                                 mask="99/99/9999 99:99:99">
                        <f:convertDateTime pattern="dd/MM/yyyy HH:mm:ss" />
                    </p:inputMask>

                    <h:outputLabel value="#{bundle.CreateCompraLabel_fornecedor}" for="fornecedor" />
                    <p:selectOneMenu id="fornecedor" value="#{compraController.selected.fornecedor}" 
                                     label="#{bundle.CreateCompraTitle_fornecedor}" required="true" 
                                     disabled="#{compraController.renderizaItensCompra}" 
                                     requiredMessage="#{bundle.CreateCompraRequiredMessage_fornecedor}">
                        <f:selectItems value="#{fornecedorController.itemsAvailableSelectOne}"/>
                    </p:selectOneMenu>

                    <p:commandButton action="#{compraController.mostrarItensCompra}"
                                     update=":formCompraItensCompraList:tableCompraItensCompra"
                                     rendered="#{(!compraController.renderizaItensCompra)}"
                                     value="Inserir Itens de Compra" 
                                     icon="ui-icon-cart" ajax="false"/>

                    <p:commandButton action="#{compraController.create}" value="#{bundle.CreateAlunoSaveLink}" 
                                     rendered="#{compraController.renderizaBotaoSalvar}" 
                                     icon="ui-icon-disk" ajax="false"/>
                    <p:commandButton value="Limpar" action="#{compraController.prepareCreate}"
                                     icon="ui-icon-document-b" ajax="false" />
                </p:panelGrid>

                <br/>
                <br/>
            </h:form>

            <h:form id="formCompraItensCompraList">

                <h:panelGroup>

                    <p:dataTable id="tableCompraItensCompra" value="#{compraController.itemsItensCompra}" var="item"
                                 style="border:solid 1px" emptyMessage="#{bundle.ListItensCompraEmpty}" 
                                 rendered="#{compraController.renderizaItensCompra}">

                        <f:facet name="header">#{bundle.ListItensCompraTitle}</f:facet>

                        <p:column>
                            <f:facet name="header">#{bundle.ListItensCompraTitle_itensCompraPK_item}</f:facet>
                            <h:outputText value="#{compraController.itemsItensCompra.rowIndex+1}" />
                        </p:column>

                        <p:column>
                            <f:facet name="header">#{bundle.ListItensCompraTitle_produto}</f:facet>
                            <h:outputText value="#{item.produto.toString()}" />
                        </p:column>

                        <p:column>
                            <f:facet name="header">#{bundle.ListItensCompraTitle_quantidade}</f:facet>
                            <h:outputText value="#{item.quantidade}" />
                        </p:column>

                        <p:column>
                            <f:facet name="header">#{bundle.ListProdutoTitle_valor}</f:facet>
                            <h:outputText value="R$ #{item.valor}" />
                            <f:facet name="footer">
                                <h:outputText value="Valor Total da Compra " />
                            </f:facet>
                        </p:column>

                        <p:column>
                            <f:facet name="header">Valor Total</f:facet>
                            <h:outputText value="R$ #{item.valorTotal}" />
                            <f:facet name="footer">
                                <h:outputText value="R$ #{compraController.valorTotalCompra}" />
                            </f:facet>
                        </p:column>

                        <p:column style="alignment-baseline: central" 
                                  rendered="#{!compraController.renderizaCondicoesCompra}">
                            <f:facet name="header">
                                <p:commandButton actionListener="#{compraController.prepareCreateItensCompra}" 
                                                 value="#{bundle.ListItensCompraCreateLink}"
                                                 update=":formCompraItensCompraCreate:pgItensCompraCreate"
                                                 oncomplete="dialogItensCompraCreate.show()" />
                            </f:facet>

                            <p:commandButton actionListener="#{compraController.prepareEditItensCompra}" 
                                             value="#{bundle.ListItensCompraEditLink}"
                                             update=":formCompraItensCompraEdit:pgItensCompraEdit"
                                             oncomplete="dialogItensCompraEdit.show()" 
                                             ajax="false"/>

                            <p:commandButton actionListener="#{compraController.destroyItensCompra}" 
                                             value="#{bundle.ListItensCompraDestroyLink}" ajax="false"/>

                        </p:column>

                        <f:facet name="footer">
                            <p:commandButton value="Definir Pagamentos"
                                             style="alignment-baseline: central"
                                             action="#{compraController.mostrarCondicoesCompra}"
                                             update=":formCompraCondicoesCompraList:tableCompraCondicoesCompra"
                                             rendered="#{(!compraController.renderizaCondicoesCompra)}"
                                             ajax="false"/>
                        </f:facet>

                    </p:dataTable>
                </h:panelGroup>

                <br/>
                <br/>

            </h:form>

            <h:form id="formCompraCondicoesCompraList">

                <h:panelGroup>
                    <p:dataTable id="tableCompraCondicoesCompra" value="#{compraController.itemsCondicoesCompra}" 
                                 var="item" style="border:solid 1px" 
                                 emptyMessage="#{bundle.ListCondicoesCompraEmpty}" 
                                 rendered="#{compraController.renderizaCondicoesCompra}">

                        <f:facet name="header">#{bundle.ListCompraTitle}</f:facet>

                        <p:column>
                            <f:facet name="header">
                                <h:outputText value="#{bundle.ListCondicoesCompraTitle_condicaoPagamento}"/>
                            </f:facet>
                            <h:outputText value="#{item.condicaoPagamento.toString()}"/>
                            <f:facet name="footer">
                                <h:outputText value="Saldo a Pagar " />
                            </f:facet>
                        </p:column>

                        <p:column>
                            <f:facet name="header">
                                <h:outputText value="#{bundle.ListCondicoesCompraTitle_condicaoPagamento}"/>
                            </f:facet>
                            <h:outputText value="#{item.valor}"/>
                            <f:facet name="footer">
                                <h:outputText 
                                    value="R$ #{(compraController.valorTotalCompra - compraController.valorTotalCondicoesCompra)}" />
                            </f:facet>
                        </p:column>

                        <p:column>
                            <f:facet name="header">
                                <h:outputText value="#{bundle.ListCondicoesCompraTitle_observacao}"/>
                            </f:facet>
                            <h:outputText value="#{item.observacao}"/>
                            <f:facet name="footer">
                                <h:outputText value="Soma dos Pagamentos " />
                            </f:facet>
                        </p:column>

                        <p:column>
                            <f:facet name="header">
                                <p:commandButton actionListener="#{compraController.prepareCreateCondicoesCompra}" 
                                                 value="#{bundle.ListCondicoesCompraCreateLink}"
                                                 update=":formCompraCondicoesCompraCreate:pgCondicoesCompraCreate"
                                                 oncomplete="dialogCondicoesCompraCreate.show()" />
                            </f:facet>

                            <p:commandButton action="#{compraController.prepareEditCondicoesCompra}" 
                                             value="#{bundle.ListCondicoesCompraEditLink}"
                                             update=":formCompraCondicoesCompraEdit:pgCondicoesCompraEdit"
                                             rendered="#{(!compraController.renderizaBotaoSalvar)}"
                                             oncomplete="dialogCondicoesCompraEdit.show()" 
                                             ajax="false"/>

                            <p:commandButton actionListener="#{compraController.destroyCondicoesCompra}" 
                                             value="#{bundle.ListCondicoesCompraDestroyLink}" ajax="false"/>

                            <f:facet name="footer">
                                <h:outputText value="R$ #{compraController.valorTotalCondicoesCompra}" />
                            </f:facet>

                        </p:column>

                    </p:dataTable>
                </h:panelGroup>

            </h:form>

            <h:form id="formCompraItensCompraCreate">

                <p:dialog header="#{bundle.CreateItensCompraTitle}" widgetVar="dialogItensCompraCreate" 
                          resizable="false" modal="true" showEffect="slide">
                    <p:panelGrid id="pgItensCompraCreate" columns="2">

                        <h:outputLabel value="#{bundle.CreateItensCompraLabel_produto}" 
                                       for="itensCompraProdutoCreate" />
                        <p:selectOneMenu id="itensCompraProdutoCreate" value="#{compraController.itensCompraSelected.produto}"
                                         label="#{bundle.CreateItensCompraTitle_produto}" required="true" 
                                         requiredMessage="#{bundle.CreateItensCompraRequiredMessage_produto}">
                            <f:selectItems value="#{produtoController.itemsAvailableSelectOne}"/>
                            <p:ajax event="change" immediate="true" listener="#{compraController.gatilharValor}" 
                                    update="itensCompraValorCreate" />
                        </p:selectOneMenu>

                        <h:outputLabel value="#{bundle.CreateProdutoLabel_valor}" 
                                       for="itensCompraValorCreate" />
                        <p:inputText id="itensCompraValorCreate" value="#{compraController.itensCompraSelected.valor}" 
                                     label="#{bundle.CreateProdutoTitle_valor}" required="true"
                                     requiredMessage="#{bundle.CreateProdutoRequiredMessage_valor}" />

                        <h:outputLabel value="#{bundle.CreateItensCompraLabel_quantidade}" 
                                       for="itensCompraQuantidadeCreate" />
                        <p:inputText id="itensCompraQuantidadeCreate" value="#{compraController.itensCompraSelected.quantidade}" 
                                     label="#{bundle.CreateItensCompraTitle_quantidade}" required="true"
                                     requiredMessage="#{bundle.CreateItensCompraRequiredMessage_quantidade}" />

                        <p:commandButton actionListener="#{compraController.createItensCompra}" 
                                         value="#{bundle.CreateItensCompraSaveLink}"
                                         update=":formCompraCreate:pgCompraCreate"
                                         oncomplete="dialogItensCompraCreate.hide()"
                                         ajax="false" />
                        <p:commandButton value="Limpar" type="reset" ajax="false"/>
                    </p:panelGrid>
                </p:dialog>

            </h:form>

            <h:form id="formCompraItensCompraEdit">

                <p:dialog header="#{bundle.EditItensCompraTitle}" widgetVar="dialogItensCompraEdit" 
                          resizable="false" modal="true" showEffect="slide">
                    <p:panelGrid id="pgItensCompraEdit" columns="2">

                        <h:outputLabel value="#{bundle.EditItensCompraLabel_produto}" 
                                       for="itensCompraProdutoEdit" />
                        <p:selectOneMenu id="itensCompraProdutoEdit" value="#{compraController.itensCompraSelected.produto}"
                                         label="#{bundle.EditItensCompraTitle_produto}" required="true" 
                                         requiredMessage="#{bundle.EditItensCompraRequiredMessage_produto}">
                            <f:selectItems value="#{produtoController.itemsAvailableSelectOne}"/>
                        </p:selectOneMenu>

                        <h:outputLabel value="#{bundle.EditItensCompraLabel_quantidade}" 
                                       for="itensCompraQuantidadeEdit" />
                        <p:inputText id="itensCompraQuantidadeEdit" value="#{compraController.itensCompraSelected.quantidade}" 
                                     label="#{bundle.EditItensCompraTitle_quantidade}" required="true"
                                     requiredMessage="#{bundle.EditItensCompraRequiredMessage_quantidade}" />

                        <p:commandButton actionListener="#{compraController.createItensCompra}" 
                                         value="#{bundle.EditItensCompraSaveLink}"
                                         update=":formCompraCreate:pgCompraCreate"
                                         oncomplete="dialogItensCompraEdit.hide()"
                                         ajax="false" />
                        <p:commandButton value="Limpar" type="reset" ajax="false"/>
                    </p:panelGrid>
                </p:dialog>

            </h:form>

            <h:form id="formCompraCondicoesCompraCreate">

                <p:dialog header="#{bundle.CreateCondicoesCompraTitle}" widgetVar="dialogCondicoesCompraCreate" 
                          resizable="false" modal="true" showEffect="slide">
                    <p:panelGrid id="pgCondicoesCompraCreate" columns="2">

                        <h:outputLabel value="#{bundle.CreateCondicoesCompraLabel_condicaoPagamento}" 
                                       for="condicoesCompraCondicaoPagamentoCreate" />
                        <p:selectOneMenu id="condicoesCompraCondicaoPagamentoCreate" 
                                         value="#{compraController.condicoesCompraSelected.condicaoPagamento}"
                                         label="#{bundle.CreateCondicoesCompraTitle_condicaoPagamento}" required="true" 
                                         requiredMessage="#{bundle.CreateCondicoesCompraRequiredMessage_condicaoPagamento}">
                            <f:selectItems value="#{condicaoPagamentoController.itemsAvailableSelectOne}"/>
                        </p:selectOneMenu>

                        <h:outputLabel value="#{bundle.CreateProdutoLabel_valor}" 
                                       for="condicoesCompraValorCreate" />
                        <p:inputText id="condicoesCompraValorCreate" value="#{compraController.condicoesCompraSelected.valor}" 
                                     label="#{bundle.CreateProdutoTitle_valor}" required="true"
                                     requiredMessage="#{bundle.CreateProdutoRequiredMessage_valor}" />

                        <h:outputLabel value="#{bundle.CreateCondicoesCompraLabel_observacao}" 
                                       for="condicoesCompraObservacaoCreate" />
                        <p:inputTextarea id="condicoesCompraObservacaoCreate" 
                                         value="#{compraController.condicoesCompraSelected.observacao}" 
                                         label="#{bundle.CreateCondicoesCompraTitle_observacao}" />

                        <p:commandButton actionListener="#{compraController.createCondicoesCompra}" 
                                         value="#{bundle.CreateCondicoesCompraSaveLink}"
                                         update=":formCompraCreate:pgCompraCreate :formCompraItensCompraList:tableCompraItensCompra"
                                         oncomplete="dialogCondicoesCompraCreate.hide()"
                                         ajax="false" />
                        <p:commandButton value="Limpar" type="reset" ajax="false"/>
                    </p:panelGrid>
                </p:dialog>

            </h:form>

            <h:form id="formCompraCondicoesCompraEdit">

                <p:dialog header="#{bundle.EditCondicoesCompraTitle}" widgetVar="dialogCondicoesCompraEdit" 
                          resizable="false" modal="true" showEffect="slide">
                    <p:panelGrid id="pgCondicoesCompraEdit" columns="2">

                        <h:outputLabel value="#{bundle.EditCondicoesCompraLabel_condicaoPagamento}" 
                                       for="condicoesCompraCondicaoPagamentoEdit" />
                        <p:selectOneMenu id="condicoesCompraCondicaoPagamentoEdit" 
                                         value="#{compraController.condicoesCompraSelected.condicaoPagamento}"
                                         label="#{bundle.EditCondicoesCompraTitle_condicaoPagamento}" required="true" 
                                         requiredMessage="#{bundle.EditCondicoesCompraRequiredMessage_condicaoPagamento}">
                            <f:selectItems value="#{condicaoPagamentoController.itemsAvailableSelectOne}"/>
                        </p:selectOneMenu>

                        <h:outputLabel value="#{bundle.EditProdutoLabel_valor}" 
                                       for="condicoesCompraValorEdit" />
                        <p:inputText id="condicoesCompraValorEdit" value="#{compraController.condicoesCompraSelected.valor}" 
                                     label="#{bundle.EditProdutoTitle_valor}" required="true"
                                     requiredMessage="#{bundle.EditProdutoRequiredMessage_valor}" />

                        <h:outputLabel value="#{bundle.EditCondicoesCompraLabel_observacao}" 
                                       for="condicoesCompraObservacaoEdit" />
                        <p:inputTextarea id="condicoesCompraObservacaoEdit" 
                                         value="#{compraController.condicoesCompraSelected.observacao}" 
                                         label="#{bundle.EditCondicoesCompraTitle_observacao}" />

                        <p:commandButton actionListener="#{compraController.createCondicoesCompra}" 
                                         value="#{bundle.EditCondicoesCompraSaveLink}"
                                         update=":formCompraCreate:pgCompraCreate :formCompraItensCompraList:tableCompraItensCompra"
                                         oncomplete="dialogCondicoesCompraEdit.hide()"
                                         ajax="false" />
                        <p:commandButton value="Limpar" type="reset" ajax="false"/>
                    </p:panelGrid>
                </p:dialog>

            </h:form>

        </ui:define>
    </ui:composition>

</html>
